package Lab01;

import com.sun.org.apache.xpath.internal.operations.Bool;

import java.util.Locale;
import java.util.Scanner;

public class Excise02 {
    public static void main(String[] args) {
        Scanner scn  = new Scanner(System.in);
        System.out.println("请输入您的身份证号码：");
        String idCard = scn.next();
        //最后一位为x的情况
        idCard = idCard.toUpperCase(Locale.ROOT);
        while(!checkId(idCard)){
            System.out.println("身份证号码输入错误！");
            System.out.println("请重新输入！");
            idCard = scn.next();
        }
        System.out.println("您输入的身份证号码为："+idCard);
        String brithday = idCard.substring(6,14);
        String bYear = brithday.substring(0,4);
        String bMouth = brithday.substring(4,6);
        String bDay = brithday.substring(7);
        System.out.println("您的生日为: "+bYear+"年"+bMouth+"月"+bDay+"日");
    }
    //身份证验证方法
    public static Boolean checkId(String str){
        boolean result = true;
        if(str.length() != 18) {
            return  false;
        }
        String tempId = str.substring(0,17);
        int[] coeff = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        char[] end = {'1','0','X','9','8','7','6','5','4','3','2'};
        int sum = 0;
        for (int i = 0; i < tempId.length(); i++)
        {
            int bye = tempId.charAt(i) - '0';
            sum += bye * coeff[i];
        }
        sum %= 11;
        if(end[sum] != str.substring(17).charAt(0)) {
            result = false;
        }

        return result;
    }
}
/*
*程序运行结果：
* 请输入您的身份证号码：
*13221321
*身份证号码输入错误！
*请重新输入！
*130133199901160910
*您输入的身份证号码为：130133199901160910
*您的生日为: 1999年01月6日
*
*/
